Skip to content

Print null checks with the precedence of === - #9291

Merged
mununki merged 1 commit into
masterfrom
print-null-checks-by-precedence
Nov 10, 2026
Merged

mununki merged 1 commit into
masterfrom
print-null-checks-by-precedence

Conversation

@mununki

@mununki mununki commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Fixes: #9290

Nullable.isNullable(e), and any other %is_nullable, compiles to e == null. Js_dump printed e below the precedence of ==, so an operand that binds more loosely lost its parentheses and the check applied to part of it: Nullable.isNullable(c ? a : b) compiled to c ? a : b == null, and Nullable.isNullable(Nullable.make(a + b)) to a + b | 0 == null. The same happened with ||, && and bitwise operands, and with a conditional from an if or switch expression, an inlined function or a let used once. An object in an arrow function's body gave a syntax error. The check itself was put in parentheses wherever it was not at the top level, as in (a == null) ? undefined : …, which came up in the review of #9288.

Is_null_or_undefined is now printed with Js_op_util.op_prec EqEqEq, as x === null is: in parentheses when the level is above that of ==, and with its operand at the left level of ===.

Tests: is_nullable_paren_test.res checks Nullable.isNullable of a conditional, an if expression, ||, an int sum and an object in an arrow body, and the check as an operand of ||, ?: and ===. Compiled with master, the module fails to load with a SyntaxError, and without the object case four of its checks return wrong values.

Checklist

  • Added a changelog fragment (see changes/README.md), or no release notes are needed and a maintainer should apply changelog:skip.
  • Added tests for the fix or feature.
  • Gave every commit a description below its subject, saying why it is made (see "Commit messages" in CONTRIBUTING.md).
  • Signed off every commit for the DCO (git commit -s).

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix Nullable.isNullable of a conditional, || or bitwise expression testing only part of it, as in Nullable.isNullable(c ? a : b).

Semantic Diff

1 commit · Full page

✏️ Print null checks with the precedence of ===

b2d750b · mununki · Nov 10, 2026, 14:29 UTC · edited

Why

Nullable.isNullable(e), and any other %is_nullable, compiles to the JS IR node Is_null_or_undefined e, which Js_dump prints as e == null. It printed e at level 1, below the precedence of ==, so an operand that binds more loosely lost its parentheses and the check applied to part of it: Nullable.isNullable(c ? a : b) compiled to c ? a : b == null, which JavaScript reads as c ? a : (b == null). The same happened to ||, && and bitwise operands, and an object in an arrow function's body gave a syntax error. It also put the whole check in parentheses wherever it was not at the top level, which the output of Stdlib_Nullable shows. Both now follow Js_op_util.op_prec EqEqEq, as x === null does: parentheses when the level is above that of ==, and the operand at its left level.

JS modified (operand of a null check)

let pick = (c, a: Nullable.t<int>, b: Nullable.t<int>) => Nullable.isNullable(c ? a : b)
let sum = (a: int, b: int) => Nullable.isNullable(Nullable.make(a + b))

js

 function pick(c, a, b) {
-  return c ? a : b == null;
+  return (
+    c ? a : b
+  ) == null;
 }
 
 function sum(a, b) {
-  return a + b | 0 == null;
+  return (a + b | 0) == null;
 }

JS modified (null check as an operand)

let equal = (a, b, eq) => Option.equal(Nullable.toOption(a), Nullable.toOption(b), eq)

js

 function equal(a, b, eq) {
-  return Stdlib_Option.equal((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), eq);
+  return Stdlib_Option.equal(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), eq);
 }

Review

  • Is the precedence of === right for ==? Both are equality operators with the same precedence and left associativity, so a context that takes a === b without parentheses takes a == null too: arguments, let initializers, ?:, ||, &&, bitwise operators and the left side of ===. The right side of === and the operand of ! still get parentheses, as in y === (x == null) and !(x == null).
  • Which checked-in outputs change? Only Stdlib_Nullable, Stdlib_TempTests and optional_external_arg_once_test, where the test of ?: loses its parentheses, and Primitive_option, where x == null as an operand of || does. No checked-in output had an operand that lost them.
  • Why does the conditional break over lines? Cond prints its own parentheses that way when it needs them, as elsewhere in the output.
  • What else prints an operand below its precedence? In prints both operands at level 0 (Dict.has evaluates the key before the dict, and a comma expression operand changes what it tests #9222), Typeof puts itself in no parentheses, and Fun ignores the level. They are separate fixes.

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 682,345,601 → 682,345,575 (±0.00%).

Generated projects: 923,215,341 → 923,214,496 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,666,073,620 → 9,666,124,075 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 25,812,251 → 25,812,323 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request b2d750b compared with base 32116b5 in this run.

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.96%. Comparing base (32116b5) to head (b2d750b).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9291   +/-   ##
=======================================
  Coverage   81.95%   81.96%           
=======================================
  Files         456      456           
  Lines       65612    65613    +1     
=======================================
+ Hits        53775    53781    +6     
+ Misses      11837    11832    -5     
Files with missing lines Coverage Δ
compiler/core/js_dump.ml 94.05% <100.00%> (+0.12%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mununki

mununki commented Nov 10, 2026

Copy link
Copy Markdown
Member Author

@codex review

@mununki
mununki requested a review from cknitt November 10, 2026 14:35
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T14:37:16.009576Z b00a4b5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b00a4b5af9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@pkg-pr-new

pkg-pr-new Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9291

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9291

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9291

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9291

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9291

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9291

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9291

commit: b2d750b

@mununki
mununki force-pushed the print-null-checks-by-precedence branch from b00a4b5 to 2f2e9cf Compare November 10, 2026 14:43
@github-actions

Copy link
Copy Markdown

Why:
    `Nullable.isNullable(e)`, and any other `%is_nullable`, compiles to the
    JS IR node `Is_null_or_undefined e`, which `Js_dump` prints as
    `e == null`. It printed `e` at level 1, below the precedence of `==`,
    so an operand that binds more loosely lost its parentheses and the
    check applied to part of it: `Nullable.isNullable(c ? a : b)` compiled
    to `c ? a : b == null`, which JavaScript reads as `c ? a : (b == null)`.
    The same happened to `||`, `&&` and bitwise operands, and an object in
    an arrow function's body gave a syntax error. It also put the whole
    check in parentheses wherever it was not at the top level, which the
    output of `Stdlib_Nullable` shows. Both now follow
    `Js_op_util.op_prec EqEqEq`, as `x === null` does: parentheses when the
    level is above that of `==`, and the operand at its left level.

JS modified (operand of a null check):
Example:
    let pick = (c, a: Nullable.t<int>, b: Nullable.t<int>) => Nullable.isNullable(c ? a : b)
    let sum = (a: int, b: int) => Nullable.isNullable(Nullable.make(a + b))
Before (js):
    function pick(c, a, b) {
      return c ? a : b == null;
    }

    function sum(a, b) {
      return a + b | 0 == null;
    }
After (js):
    function pick(c, a, b) {
      return (
        c ? a : b
      ) == null;
    }

    function sum(a, b) {
      return (a + b | 0) == null;
    }

JS modified (null check as an operand):
Example:
    let equal = (a, b, eq) => Option.equal(Nullable.toOption(a), Nullable.toOption(b), eq)
Before (js):
    function equal(a, b, eq) {
      return Stdlib_Option.equal((a == null) ? undefined : Primitive_option.some(a), (b == null) ? undefined : Primitive_option.some(b), eq);
    }
After (js):
    function equal(a, b, eq) {
      return Stdlib_Option.equal(a == null ? undefined : Primitive_option.some(a), b == null ? undefined : Primitive_option.some(b), eq);
    }

Review: Is the precedence of `===` right for `==`? Both are equality
    operators with the same precedence and left associativity, so a
    context that takes `a === b` without parentheses takes `a == null`
    too: arguments, `let` initializers, `?:`, `||`, `&&`, bitwise operators
    and the left side of `===`. The right side of `===` and the operand of
    `!` still get parentheses, as in `y === (x == null)` and
    `!(x == null)`.
Review: Which checked-in outputs change? Only `Stdlib_Nullable`,
    `Stdlib_TempTests` and `optional_external_arg_once_test`, where the
    test of `?:` loses its parentheses, and `Primitive_option`, where
    `x == null` as an operand of `||` does. No checked-in output had an
    operand that lost them.
Review: Why does the conditional break over lines? `Cond` prints its own
    parentheses that way when it needs them, as elsewhere in the output.
Review: What else prints an operand below its precedence? `In` prints both
    operands at level 0 (#9222), `Typeof` puts itself in no parentheses,
    and `Fun` ignores the level. They are separate fixes.

Signed-off-by: mununki <woonki.moon@gmail.com>
@mununki
mununki force-pushed the print-null-checks-by-precedence branch from 2f2e9cf to b2d750b Compare November 10, 2026 16:16
@mununki
mununki merged commit e8ba9f9 into master Nov 10, 2026
22 checks passed
@mununki
mununki deleted the print-null-checks-by-precedence branch November 10, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nullable.isNullable of a conditional or an arithmetic expression drops the parentheses it needs

2 participants